package com.oocl.producer.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import com.oocl.producer.pojo.Book;

/**
 * XML Util
 * @author GANAB
 *
 */
public class XMLUtil {
	private static String rootName = "books";
	private static Element root = new Element(rootName);
	private static Document doc = new Document(root);
	
	public static void outputBookXmlToFile(Book book){
		Element node = new Element("book");
		node.setAttribute("id",""+book.getId());
		node.addContent(new Element("name").setText(book.getName()));
		node.addContent(new Element("pubDate").setText(book.getPubDate().getTime()+""));
		node.addContent(new Element("author").setText(book.getAuthor()));
		node.addContent(new Element("price").setText(""+book.getPrice()));
		node.addContent(new Element("isNew").setText(book.getIsNew()));
		node.addContent(new Element("publisher").setText(book.getPublisher()));
		root.addContent(node);
		XMLOutputter xmlOut = new XMLOutputter();
		try {
			xmlOut.output(doc, new FileOutputStream("books.xml"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
